<!DOCTYPE html>
<html>
<head>
	<meta name="mobile-web-app-capable" content="yes">
	<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
	<meta charset="UTF-8">
    <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
    <script src="../../script/jquery.min.js"></script>
	<script src="../../script/semantic/semantic.min.js"></script>
    <script src="../arsm/js/moment.min.js"></script>
    <style>
        .hidden{
            display:none;
        }
    </style>
</head>
<body>
    <div class="ui container">
        <div class="ui basic segment">
            <div class="ui header">
                <i class="calendar alternate outline icon"></i>
                <div class="content">
                    System Schedular
                    <div class="sub header">Schedule tasks to run while you are offline</div>
                </div>
            </div>
        </div>
       
        <div class="ui divider"></div>
        <table class="ui celled striped table">
            <thead>
                <tr>
                    <th colspan="4">
                        Scheduled Tasks
                    </th>
                </tr>
                <tr>
                    <th>
                        Script
                    </th>
                    <th>
                        Location
                    </th>
                    <th>
                        Interval
                    </th>
                    <th>
                        Base Time
                    </th>
                </tr>
            </thead>
            <tbody id="scheduleList">
               
            </tbody>
          </table>
          <button id="schedulerButton" class="ui button" onclick="openSystemScheudler();">Open System Scheduler</button>
    </div>
    <script>
        initScheduleList();
        function initScheduleList(){
            $.ajax({
                url: "../../system/arsm/aecron/list",
                data: {},
                success: function(data){
                    $("#scheduleList").html("");
                    if (data.length == 0){
                        $("#scheduleList").append(`<tr>
                            <td colspan="4">
                                No registered schedule.
                            </td>
                        </tr>`);
                    }else{
                        data.forEach(task => {
                            var scriptName = task.ScriptFile.split("/").pop();
                            $("#scheduleList").append(`<tr>
                                <td class="collapsing">
                                    <i class="code file outline icon"></i> ${scriptName}
                                </td>
                                <td>${task.ScriptFile}</td>
                                <td class="right aligned collapsing">Every ${parseSecondsToHumanReadableFormat(task.ExecutionInterval)}</td>
                                <td class="right aligned collapsing">${moment.unix(task.BaseTime).format('LLL')}</td>
                            </tr>`);
                        });
                    }
                }
            });
        }

        function parseSecondsToHumanReadableFormat(seconds){
            seconds = Number(seconds);
            var d = Math.floor(seconds / (3600*24));
            var h = Math.floor(seconds % (3600*24) / 3600);
            var m = Math.floor(seconds % 3600 / 60);
            var s = Math.floor(seconds % 60);

            var dDisplay = d > 0 ? d + (d == 1 ? " day " : " days ") : "";
            var hDisplay = h > 0 ? h + (h == 1 ? " hour " : " hours ") : "";
            var mDisplay = m > 0 ? m + (m == 1 ? " minute " : " minutes ") : "";
            var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
            return dDisplay + hDisplay + mDisplay + sDisplay;
        }


        //Check if the user has access right to the scheduler
        $.ajax({
            url : "../../system/modules/list",
            data: {},
            success: function(data){
                var allowAccess = false;
                data.forEach(moduleInfo => {
                    if (moduleInfo.Name == "Tasks Scheduler"){
                        //This user has access to task scheduler
                        allowAccess = true;
                    }
                });

                if (!allowAccess){
                    $("#schedulerButton").hide();
                }
            }
        })

        function openSystemScheudler(){
            ao_module_newfw({
                url: "SystemAO/arsm/scheduler.html",
                appicon: "SystemAO/arsm/img/scheduler.png",
                title: "Task Scheduler"
            });
        }


    </script>
</body>
</html>